/**
 * 店铺中心js
 */
define('module/store', function (require, exports, module){

  'use strict';
  
  var    wx = require('wx'),
   loadtype = require('zepto.loadtype');
  
  var store = {
        images: {
            localId: []
        },
        init:function(){
            var _self = this;
            
            
        },
        /*商户登录页*/
        login:function(){
            //刷新验证码
            var verifyimg = $(".verifyimg").attr("src");
            $(".reloadverify").click(function(){
                if(verifyimg.indexOf('?')>0){
                    $(".verifyimg").attr("src", verifyimg+'&random='+Math.random());
                }else{
                    $(".verifyimg").attr("src", verifyimg.replace(/\?.*$/,'')+'?'+Math.random());
                }
            });
            
            $("form").submit(function(){
                var self = $(this);
                if($('input[name="username"]').val() == ''){
                    $.ui.alert('请填写商家登录名');
                    return false;
                }
                if($('input[name="password"]').val() == ''){
                    $.ui.alert('请输入密码');
                    return false;
                }
                if($('input[name="verify"]').val() == ''){
                    $.ui.alert('请输入验证码');
                    return false;
                }
                $.post(self.attr("action"), self.serialize(), function(data){
                    if(data.status){
                        window.location.href = data.url;
                      }else{
                          $.ui.alert(data.info);
                        //刷新验证码
                        $(".reloadverify").click();
                      }
                }, "json");
                
                return false;
              });
        },
        /* 商品管理 */
        item:function(){
            $('.J_item').on('click','.active .J_up',function(){
                var itemId = $(this).attr('data-id');
                var _this = $(this);
                var url = $('.mctlist9').attr('data-url');
                $.get(url,{ids:itemId,status:1},function(res){
                    if(res.status == 1){
                        _this.parents('li.active').removeClass('active');
                    }else{
                        $.ui.alert(res.info);
                    }
                });
            });
            
            $('.J_item').on('click','.J_off',function(){
                var itemId = $(this).attr('data-id');
                var _this = $(this);
                var url = $('.mctlist9').attr('data-url');
                $.get(url,{ids:itemId,status:0},function(res){
                    if(res.status == 1){
                        _this.parent().parent().parent('li').addClass('active');
                    }else{
                        $.ui.alert(res.info);
                    }
                });
            });
            
            $('.J_item').on('click','.J_edit',function(){
                var url = $(this).attr('data-url');
                if(url != ''){
                    window.location.href=url;
                }
            });
            
            /* 编辑商品价格和库存 */
            $('.J_spec_set').on('click',function(){
                $('.goods-pup').toggle();
            });
            
            $('.J_cancel').on('click',function(){
                $('.goods-pup').hide();
            });
            
            $('.J_spec_sub').on('click',function(){
                $('.goods-pup').hide();
                var price = $('input[name="price"]').val();
                var stock = $('input[name="stock"]').val();
                if(price < 0 || stock < 0){
                    $.ui.error('价格和库存设置错误！');
                    return false;
                }
                var url = $('.J_spec_form').attr('action');
                $.post(url,$('.J_spec_form').serialize(),function(res){
                    if(res.status != 0){
                        if($('.J_item_spec').length > 0){
                            $('.J_spec_list .J_price_list').html(price);
                            $('.J_spec_list .J_stock_list').html(stock);
                        }
                        $.ui.success(res.info);
                    }else{
                        $.ui.error(res.info);
                    }
                },'json');
                return false;
            });
        },
        // 选择图片
        chooseImage: function(fun){
          var _self = this;
          //初始化配置
          wx.config(C.WECHAT_CONFIG);
          //选择图片
          wx.ready(function(){
            wx.chooseImage({
              success: function(res){
                var localIds = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
                _self.images.localId = localIds;
                $.ui.loading('正在启动上传...');
                _self.uploadImage(fun);
              },
              fail: function(res){
                //alert(JSON.stringify(res));
              }
            });
          });
        },
        //上传图片
        uploadImage: function(fun){
          var _self = this;
          $.ui.loading('正在上传...');
          wx.ready(function(){
            wx.uploadImage({
              localId: _self.images.localId[0],
              success: function(res){
                if(typeof fun === 'function'){
                  fun(_self.images.localId[0], res.serverId);
                }
              },
              fail: function(res){
                //alert(JSON.stringify(res));
              }
            });
          });
        },
        /* 加载地区 */
        areaType: function(aObject){
            $(aObject.obj).Loadtype({
              type: 'Area',
              name1: 'province',
              name2: 'city',
              name3: 'district',
              value1: aObject.value1,
              value2: aObject.value2,
              value3: aObject.value3
            });
        }
  };
  
  module.exports = store;
});